package cn.jiyun.controller;

import cn.jiyun.pojo.User;
import cn.jiyun.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserController {
    //注入依赖
    @Resource
    private UserService userService;

    //全查
    @RequestMapping("/findAll")
    public String findAll(Model model,String name) {
        List<User> all = userService.findAll(name);
        model.addAttribute("list", all);
        model.addAttribute("name",name);
        return "user";
    }

    //去到添加页面
    @GetMapping("/toAdd")
    public String toAdd() {
        return "add";
    }

    //添加
    @RequestMapping("/add")
    public String add(User user) {
        userService.add(user);
        return "redirect:/findAll";
    }

    //回显
    @GetMapping("/findById")
    public String findById(String id, Model model) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return "update";
    }

    //修改
    @RequestMapping("/update")
    public String update(User user) {
        userService.update(user);
        return "redirect:/findAll";
    }

    //删除
    @GetMapping("/del")
    public String del(String id) {
        userService.del(id);
        return "redirect:/findAll";
    }

}
